﻿using System;
using System.Data;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Web.UI;

namespace AMS.Web.Report
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private readonly CastleAccess m_Acc = new CastleAccess();
        private ObjXmlReader m_XmlReader;
        private string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Report\\ReportDataCollectSetting.xml"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindPgList);
            m_XmlReader = new ObjXmlReader(SettingXmlPath);
            if (Request.Cookies["Code"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if(!IsPostBack)
            {
                ListGridInfo.BindGrid();
            }
           
        }



        private object BindPgList()
        {
          
            var dt = new DataTable();
            dt.Columns.Add("Department", typeof (string));
            dt.Columns.Add("TotalCount", typeof (string));
           
            XmlNodeList nodeList = m_XmlReader.GetNodeList("Setting/WoTypeNameEnum/Item");
            if(nodeList != null && nodeList.Count > 0)
            {
               
                foreach (var node in nodeList)
                {
                  
                  var value = m_XmlReader.GetNodeAttributeValue((XmlNode)node, "Value", true);
                  dt.Columns.Add(value, typeof(string));
              
                    
                }
            }
            string sql = @"select * from ( select fug.Name Department,count(*) Count from  AmsCore_AssetInfo ai 
left outer join dbo.Frame_UserGroup fug on fug.Id = ai.UserGroupId group by fug.Name ) b";
           var dt1 = m_Acc.GetDataTableBySql(sql);
            if(dt1 != null && dt1.Rows.Count > 0)
            {
                
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    DataRow row = dt.NewRow();
                    row["Department"] = dt1.Rows[i]["Department"].ToString();
                    row["TotalCount"] = dt1.Rows[i]["Count"].ToString();
              
                    XmlNodeList nodeList2 = m_XmlReader.GetNodeList("Setting/WoTypeNameEnum/Item");
                     if (nodeList2 != null && nodeList2.Count > 0)
                     {
                         foreach (var node in nodeList2)
                         {
                             string columnName;
                             var value = m_XmlReader.GetNodeAttributeValue((XmlNode) node, "Value", true);
                             columnName = value == "Raback" ? "Return" : value;
                             string sql2 = "select count(*) as " + columnName + " from dbo.AmsWorkBill_WoCommand cmd " +
                            " left outer join dbo.AmsCore_AssetInfo ai on ai.Id = cmd.AssetInfoId left outer join dbo.Frame_UserGroup fug on fug.Id = ai.UserGroupId" +
                            " join dbo.AmsWorkBill_WoInfo wo on cmd.WoCode = wo.Code where fug.Name='" + dt1.Rows[i]["Department"] + "' and wo.TypeName='" + value + "'";
                             var dt2 = m_Acc.GetDataTableBySql(sql2);
                             if(dt2 != null && dt2.Rows.Count > 0)
                             {
                                 row[value] = dt2.Rows[0][value].ToString();
                             }
                         }
                     }
                    dt.Rows.Add(row);
                 }
            }
            DataRow rowEnd = dt.NewRow();
            rowEnd["Department"] = "汇总";
            rowEnd["TotalCount"] = ReportQueryHelp.GetTotalCount();
            rowEnd["Consuming"] = ReportQueryHelp.GetCountByTypeName("Consuming");
            rowEnd["Maintain"] = ReportQueryHelp.GetCountByTypeName("Maintain");
            rowEnd["Borrow"] = ReportQueryHelp.GetCountByTypeName("Borrow");
            rowEnd["Reback"] = ReportQueryHelp.GetCountByTypeName("Return");
            rowEnd["BackPosition"] = ReportQueryHelp.GetCountByTypeName("BackPosition");
            rowEnd["Allocation"] = ReportQueryHelp.GetCountByTypeName("Allocation");
            rowEnd["IsUsed"] = ReportQueryHelp.GetCountByTypeName("IsUsed");
            rowEnd["IsIdleness"] = ReportQueryHelp.GetCountByTypeName("IsIdleness");
            rowEnd["Scrap"] = ReportQueryHelp.GetCountByTypeName("Scrap");
            rowEnd["Damage"] = ReportQueryHelp.GetCountByTypeName("Damage");
            dt.Rows.Add(rowEnd);
            return dt;
        }

    }
}